<template>
	<SearchForm
		:form-state="searchFormState"
		:form-fields="[
			{ label: '关键字', name: 'searchKey', span: 8 }
		]"
		@search="table.refresh(true)"
		@reset="handleReset"
		ref="searchFormRef"
	>
		<template #searchKey="{ formState }">
			<a-input v-model:value="formState.searchKey" placeholder="请输入关键字" allowClear />
		</template>
	</SearchForm>
	<s-table ref="table" :columns="columns" :data="loadData" :alert="false" bordered :row-key="(record) => record.id" :tool-config="toolConfig" :operator-config="operatorConfig">
		<template #bodyCell="{ column, record }">
			<template v-if="column.key === 'action'">
				<a-space>
					<a @click="form.onOpen(record)">编辑</a>
					<a-divider type="vertical" />
					<a-popconfirm title="确定要删除此配置吗？" @confirm="deleteConfig(record)">
						<a-button type="link" danger size="small">删除</a-button>
					</a-popconfirm>
				</a-space>
			</template>
		</template>
	</s-table>
	<Form ref="form" @successful="table.refresh(true)" />
</template>
<script setup name="sysModule">
	import { message } from 'ant-design-vue'
	import Form from './form.vue'
	import configApi from '@/api/dev/configApi'
	let searchFormState = reactive({})
	const formRef = ref()
	const searchFormRef = ref()
	const table = ref()
	let form = ref()

	// 重置搜索表单
	const handleReset = () => {
		searchFormRef.value?.resetFields()
	}

	// 操作按钮配置
	const operatorConfig = computed(() => [
		{
			text: '新增配置',
			icon: 'fa fa-plus',
			onClick: () => form.value.onOpen()
		}
	])
	const toolConfig = { refresh: true, height: true, columnSetting: false, striped: false }
	const columns = [
		{
			title: '配置键',

			dataIndex: 'configKey',
			ellipsis: true
		},
		{
			title: '配置值',

			dataIndex: 'configValue',
			ellipsis: true
		},
		{
			title: '备注',

			dataIndex: 'remark'
		},
		{
			title: '排序',

			dataIndex: 'sortCode',
			width: 100,
			sorter: true
		},
		{
			title: '操作',

			key: 'action',
			width: 280,
			scopedSlots: { customRender: 'action' }
		}
	]
	const loadData = (parameter) => {
		return configApi.configPage(Object.assign(parameter, searchFormState)).then((res) => {
			return res
		})
	}
	// 删除
	const deleteConfig = (record) => {
		let params = {
			id: record.id
		}
		configApi.configDetail(params).then(() => {
			table.value.refresh(true)
		})
	}
</script>
